using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using _MetalSlug.Form.ScreenSystem;
using _MetalSlug.Mementoes;
using _MetalSlug.MyUtilities;

namespace _MetalSlug
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        MetalSlug _metalSlug = new MetalSlug();
        Video myPlayBack;
        VideoPlayer videoPlayer = new VideoPlayer();
        SpriteFont Font1;
        SpriteFont Font2;
        private bool isPlayback = true;
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";            
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            UserInput.CurrentKeyBoard = UserInput.OldKeyBoard = Keyboard.GetState();
            GameUtil.ScreenSize = new Vector2(this.Window.ClientBounds.Width, this.Window.ClientBounds.Height);
            GameUtil.SetContent(Content);
            GameUtil.GamePivot = new MyPivot();
            base.Initialize();
            CareTaker.GetInstance();
            
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            myPlayBack = GameUtil.LoadVideo("Playback/trailer");
            Font1 = GameUtil.LoadFont("Font/SpriteFont1");
            Font2 = GameUtil.LoadFont("Font/SpriteFont2");
             _metalSlug.LoadContent(Font2);
            videoPlayer.Play(myPlayBack);
        }



        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || GameManager.GetInstance().GetCurrentScreen() == null)
            {
                if(!CareTaker.GetInstance().IsNull())
                {
                    CareTaker.GetInstance().SaveGameToFile();
                }
                this.Exit();
            } 

            UserInput.CurrentKeyBoard = Keyboard.GetState();
            if (gameTime.TotalGameTime.Seconds > 0)
            {
                //pause menu screen 
                if (isPlayback)
                {
                    if (videoPlayer.PlayPosition.Seconds == 22)
                    {
                        videoPlayer.Pause();
                    }

                    if(UserInput.isKeyClicked(Keys.Enter))
                    {
                        isPlayback = false;
                        videoPlayer.Stop();
                        videoPlayer.Dispose();
                    }
                }
                else
                {
                    _metalSlug.Update(gameTime);
                }
            }

            base.Update(gameTime);
            UserInput.OldKeyBoard = UserInput.CurrentKeyBoard;
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            if (isPlayback)
            { 
                spriteBatch.Draw(videoPlayer.GetTexture(),
                                 new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White);
                if (gameTime.TotalGameTime.Seconds > 10 && videoPlayer.State != MediaState.Paused)
                {
                    spriteBatch.DrawString(Font1, "Click mouse or Press 'Enter' to quit", new Vector2(320f, 0f), Color.White);

                }
                if (videoPlayer.State == MediaState.Paused)
                {
                    spriteBatch.DrawString(Font2, "Press Enter to Play", new Vector2(320f, 420f), Color.White);
                }
            }
            else
            {
                _metalSlug.Draw(spriteBatch);
            }
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
